package com.wei.controller.front;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.wei.entity.Course;
import com.wei.entity.Teacher;
import com.wei.service.CourseService;
import com.wei.service.TeacherService;
import com.wei.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author wjx
 * @ClassName ShowFrontController
 * @description: TODO
 * @date 2022-12-02 13:12
 * @version: 1.0
 */
@RestController
@RequestMapping("/eduservice/front")

public class ShowFrontController {

    @Autowired
    private CourseService courseService;

    @Autowired
    private TeacherService teacherService;

    /**
     * 返回首页数据：
     * 1.返回8条课程信息
     * 2.返回4条讲师信息
     *
     * @return
     */
    @GetMapping("/getFrontData")
    @Cacheable(value = "frontData", key = "'indexCourseTeacher'")
    public R getFrontData() {

        LambdaQueryWrapper<Course> queryWrapper1 = new LambdaQueryWrapper<>();
        queryWrapper1.orderByDesc(Course::getViewCount).last("limit 8");

        List<Course> courseList = courseService.list(queryWrapper1);

        LambdaQueryWrapper<Teacher> queryWrapper2 = new LambdaQueryWrapper<>();
        queryWrapper2.orderByDesc(Teacher::getSort).last("limit 4");

        List<Teacher> teacherList = teacherService.list(queryWrapper2);

        return R.ok().data("courseList", courseList).data("teacherList", teacherList);
    }

}
